﻿Imports DotNetNuke.Light.Common

Public Class MessagingExtension
    Implements IExtension

    <Light.Server.SecurityRole(RoleName:="Registered Users")> _
    Public Function GetUserInbox(ByVal Input As String) As String
        Dim mc As New DotNetNuke.Services.Messaging.MessagingController
        Dim inbox As System.Collections.Generic.List(Of DotNetNuke.Services.Messaging.Data.Message) = mc.GetUserInbox(UserInfo.PortalID, UserInfo.UserID, 1, 10)

        Dim msgList As New System.Collections.Generic.List(Of SimpleMessage)()

        For Each m As DotNetNuke.Services.Messaging.Data.Message In inbox
            msgList.Add(SimpleMessage.FromMessage(m))
        Next

        Return Light.Server.JSON.Serialize(msgList, GetType(System.Collections.Generic.List(Of SimpleMessage)))
    End Function

    <Light.Server.SecurityRole(RoleName:="Registered Users")> _
    Public Function GetInboxCount(ByVal Input As String) As String
        Dim mc As New DotNetNuke.Services.Messaging.MessagingController
        Return mc.GetInboxCount(UserInfo.PortalID, UserInfo.UserID).ToString()
    End Function

    <Light.Server.SecurityRole(RoleName:="Registered Users")> _
    Public Function GetNewMessageCount(ByVal Input As String) As String
        Dim mc As New DotNetNuke.Services.Messaging.MessagingController
        Return mc.GetNewMessageCount(UserInfo.PortalID, UserInfo.UserID).ToString()
    End Function

    <Light.Server.SecurityRole(RoleName:="Registered Users")> _
    Public Function GetMessageByID(ByVal Input As String) As String
        Dim msgId As String = Convert.ToInt32(Input)
        Dim mc As New DotNetNuke.Services.Messaging.MessagingController

        Dim msg As DotNetNuke.Services.Messaging.Data.Message = mc.GetMessageByID(UserInfo.PortalID, UserInfo.UserID, msgId)


        Return Light.Server.JSON.Serialize(SimpleMessage.FromMessage(msg), GetType(SimpleMessage))


    End Function


    Public ReadOnly Property AssemblyName As String Implements IExtension.AssemblyName
        Get
            Return Me.GetType().Assembly.FullName
        End Get
    End Property


    Public ReadOnly Property ExtensionID As System.Guid Implements IExtension.ExtensionID
        Get
            Return New Guid("343B0274-6481-49A0-A736-642370C57FC1")
        End Get
    End Property

    Private tName As String
    Public ReadOnly Property TypeName As String Implements IExtension.TypeName
        Get
            Return Me.GetType().FullName
        End Get
    End Property



    Private uInfo As DotNetNuke.Entities.Users.UserInfo
    Public Property UserInfo() As DotNetNuke.Entities.Users.UserInfo Implements IExtension.UserInfo
        Get
            Return uInfo
        End Get
        Set(ByVal value As DotNetNuke.Entities.Users.UserInfo)
            uInfo = value
        End Set
    End Property

End Class

Public Class SimpleMessage
    Public ID As Integer
    Public Subject As String
    Public Body As String
    Public DateSent As DateTime
    Public FromUser As String

    Public Shared Function FromMessage(ByVal Msg As DotNetNuke.Services.Messaging.Data.Message) As SimpleMessage
        Dim sm As New SimpleMessage
        sm.Body = Msg.Body
        sm.Subject = Msg.Subject
        sm.DateSent = Msg.MessageDate
        sm.ID = Msg.MessageID
        sm.FromUser = Msg.FromUserName
        Return sm
    End Function
End Class

